package cn.hcbycloud.auth.front.controller;

import cn.hcbycloud.auth.bo.UserRegisterBO;
import cn.hcbycloud.auth.bo.UserRegisterResultBO;
import cn.hcbycloud.auth.front.converter.FrontUserConverter;
import cn.hcbycloud.auth.front.dto.FrontRegisterUserRequestDTO;
import cn.hcbycloud.auth.front.dto.FrontRegisterUserResponseDTO;
import cn.hcbycloud.auth.service.UserService;
import cn.hcbycloud.common.core.config.IgnoreUrlsConfig;
import cn.hcbycloud.common.core.exception.CustomizeException;
import cn.hcbycloud.common.core.vo.Result;
import cn.hcbycloud.common.jwt.vo.LoginInfo;
import cn.hcbycloud.common.permission.annotation.RolesRequires;
import cn.hcbycloud.common.permission.constant.Relation;
import cn.hcbycloud.common.permission.utils.AccessContext;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户基本信息表
 *
 * @author gaoheng
 * @date 2022-01-24 14:52:06
 */
@RestController
@RequestMapping("/front/user")
@AllArgsConstructor
public class FrontUserController {
    private final UserService userService;
    private final FrontUserConverter frontUserConverter;

    /**
     * 新用户个人客户注册
     *
     * @param requestDTO 参数
     * @return Result<Void>
     */
    @PostMapping("/register/{tenantCode}")
    public Result<FrontRegisterUserResponseDTO> registerUser(@Valid @PathVariable("tenantCode") String tenantCode, @RequestBody FrontRegisterUserRequestDTO requestDTO) {
        // 校验用户注册信息
        UserRegisterBO userRegisterBO = frontUserConverter.requestToModelOfPersonCust(requestDTO);
        // 开始注册用户及对应客户信息
        UserRegisterResultBO userRegisterResultBO = null;
        try {
            userRegisterResultBO = userService.registerUserOfPersonCust(userRegisterBO,tenantCode);
        } catch (Exception e) {
            throw new CustomizeException("新用户注册异常");
        }
        return Result.ok(frontUserConverter.modelToResponse(userRegisterResultBO));
    }

    @PostMapping("/xxx")
    @RolesRequires(value={"regularUser","operationsStaff"},relation= Relation.OR)
    public Result<String> test() {
        System.out.print(IgnoreUrlsConfig.NACOS_IGNORE_URLS);
        LoginInfo loginInfo = AccessContext.getLoginInfo();
        return Result.ok("测试");
    }
}